package ar.com.ford.it.fullEconomics.interfaces.threadExecutor;

import java.net.InetAddress;
import java.util.Calendar;
import java.util.Date;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import com.ford.it.properties.PropertyGroup;
import com.ford.it.properties.PropertyManager;
import commonj.timers.TimerListener;
import commonj.timers.TimerManager;

public class ThreadListener implements ServletContextListener {

	private static final int WAIT_TIME 		= 1000 * 10; 
	
	public void contextDestroyed(ServletContextEvent arg0) {
	}

	public void contextInitialized(ServletContextEvent arg0) {

		try {
					
			// Obtener valor de dynaprop
			PropertyManager pm = PropertyManager.getInstance();
			pm.loadFromDB("batchprocessing_fullee");
			PropertyGroup pg = (PropertyGroup) pm.getGroup("batchprocessing_fullee");
			
			String strTimerServer = pg.getString("Server_To_Run");
			InetAddress inet = InetAddress.getLocalHost();
			System.out.println("El servidor es: " + inet.getHostName());
			
			if (!inet.getHostName().equalsIgnoreCase(strTimerServer))
				return;
			
			System.out.println("-- starting Timer Listener --");
			System.out.println("fecha en el Server: " + (new Date()).toString());

			InitialContext ctx = new InitialContext();
			TimerManager mgr = (TimerManager) ctx.lookup("java:comp/env/timer/MyTimer"); //
			TimerListener listener = null;
			
			listener = new TimerProductionVolumesMix();
			taskSchedule (pg, "Proc_MixTime",mgr, listener);

			listener = new TimerInterfaceBMSupplier();
			taskSchedule (pg, "InterfaceBMSupplier",mgr, listener);
			
			listener = new TimerInterfaceBMRunchee();
			taskSchedule (pg, "InterfaceBMRunchee",mgr, listener);

			listener = new TimerInterfaceWipsParts();
			taskSchedule (pg, "InterfaceWipsParts",mgr, listener);
			
			listener = new TimerInterfaceWipsPrice();
			taskSchedule (pg, "InterfaceWipsPrice",mgr, listener);
			
			listener = new TimerFtpFileDownloader();
			taskSchedule (pg, "FtpFileDownloader",mgr, listener);
			
			System.out.println(" finish scheduling ");
			} catch (NamingException e) {
				e.printStackTrace();
			} catch (Exception e) 
			{
				e.printStackTrace();
			}

	}
	
	private void taskSchedule (PropertyGroup pg, String propertyName,TimerManager mgr, TimerListener listener)
	{
		String dynaString = pg.getString(propertyName);
		if (null!=dynaString && !dynaString.equals(""))
		{ 	
			String[] cicles = dynaString.split(";");
			for (int i=0;i<cicles.length;i++)
			schedule (cicles[i], mgr, listener);		
		}
		
		
	}
	
	private void schedule (String strTime, TimerManager mgr, TimerListener listener)
	{
		String[] times = strTime.split(":");
		
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.AM_PM, Calendar.AM);
		
		int hora     = Integer.parseInt(times[0]);
		int minutos  = 0;
		int segundos = 0;
		
		if (times.length > 1) {
			minutos = Integer.parseInt(times[1]);
			
			if (times.length > 2) {
				segundos = Integer.parseInt(times[2]);
			}
		}
		
		cal.set(Calendar.HOUR,hora);
		cal.set(Calendar.MINUTE,minutos);
		cal.set(Calendar.SECOND,segundos);
		
		if (cal.before(Calendar.getInstance()))
				cal.add(Calendar.DATE, 1); //
																	// -- cada 24hs --
		commonj.timers.Timer t =  mgr.schedule(listener, cal.getTime(),(1000*60*60*24)); 
		System.out.println(listener.toString() +  " process scheduled at " + cal.getTime());
		
	}

	
	

}
